package com.snail.readyvr.rest;

import com.snail.readyvr.event.GetAccountInfo;
import com.snail.readyvr.mvp.model.AccountInfo;
import com.snail.readyvr.util.Constant;
import com.snail.readyvr.util.EventBus;
import com.snail.readyvr.util.Utils;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * Created by MRKING on 2017/1/18.
 */

public class GetAccountInfoUtils {

    public static void getAccountInfo(LoginRestClient loginRestClient) {

        loginRestClient.getAccountInfo(Utils.token).map(new HttpResultFunc<AccountInfo>(RestApiUtils.GET_ACCOUNT_INFO))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<AccountInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        EventBus.getDefault().post(Constant.BusTag.GET_ACCOUNT_TAG, new GetAccountInfo(GetAccountInfo.TYPE_FAILE));
                    }

                    @Override
                    public void onNext(AccountInfo accountInfo) {
                        if(accountInfo != null) {
                            Utils.myAccountInfo = accountInfo;
                            EventBus.getDefault().post(Constant.BusTag.GET_ACCOUNT_TAG, new GetAccountInfo(GetAccountInfo.TYPE_SUCCESS));
                        }else {
                            EventBus.getDefault().post(Constant.BusTag.GET_ACCOUNT_TAG, new GetAccountInfo(GetAccountInfo.TYPE_FAILE));
                        }
                    }
                });
    }
}
